import { _decorator, Component, Node, log, screen, v3, view, UITransform, Enum } from 'cc';
import { UIMgr } from '../../../Lobby/Scripts/UIMgr';
const { ccclass, property } = _decorator;

export enum FixType {
    FitHeight,
    FitWidth,
}
Enum(FixType);

export enum ResizeMode {
    Stretch,
    FixAspect
}
Enum(ResizeMode);

enum LifeCycleFunc {
    ON_LOAD,
    START,
}
Enum(LifeCycleFunc)

@ccclass('ResizeLengthOverMax')
export class ResizeLengthOverMax extends Component {

    @property({ type: FixType })
    fixType: FixType = FixType.FitHeight;

    @property({ type: ResizeMode })
    resizeMode: ResizeMode = ResizeMode.Stretch;

    @property({ type: LifeCycleFunc })
    lifeCycleFunc: LifeCycleFunc = LifeCycleFunc.START;

    @property
    exRate = 1;

    @property
    maxLength = 1600;

    protected onLoad(): void {
        if (this.lifeCycleFunc == LifeCycleFunc.ON_LOAD) this.go();
    }

    protected start(): void {
        if (this.lifeCycleFunc == LifeCycleFunc.START) this.go();
    }

    go() {
        const size = view.getVisibleSize();
        // UIMgr.getInstance().AlertTxt(`size:(${size.width},${size.height}) maxLength:${this.maxLength}`);
        // const designSize = view.getDesignResolutionSize();

        if (this.fixType == FixType.FitHeight) {
            if (size.width > this.maxLength) {
                let k = size.width / this.maxLength;
                if (this.resizeMode == ResizeMode.Stretch) {
                    this.node.getComponent(UITransform).width *= k * this.exRate;
                    this.node.getComponent(UITransform).height *= this.exRate;
                } else if (this.resizeMode == ResizeMode.FixAspect) {
                    this.node.getComponent(UITransform).width *= k * this.exRate;
                    this.node.getComponent(UITransform).height *= k * this.exRate;
                }
            }
        } else if (this.fixType == FixType.FitWidth) {
            if (size.height > this.maxLength) { // 长度比1600还大
                let k = size.height / this.maxLength;
                // UIMgr.getInstance().AlertTxt(`height:${size.height} maxLength:${this.maxLength} k:${k}`);
                if (this.resizeMode == ResizeMode.Stretch) {
                    this.node.getComponent(UITransform).width *= this.exRate;
                    this.node.getComponent(UITransform).height *= k * this.exRate;
                } else if (this.resizeMode == ResizeMode.FixAspect) {
                    this.node.getComponent(UITransform).width *= k * this.exRate;
                    this.node.getComponent(UITransform).height *= k * this.exRate;
                }
            }
        }
    }
}


